import { _decorator, Component, Label, Node, Sprite, SpriteFrame, Texture2D } from 'cc';
import { ResLoadHelper } from '../../Frame/Script/resLoadHelper';
import { App } from '../app';
import { NetInfo } from '../core/NetInfo';
const { ccclass, property } = _decorator;

@ccclass('OverItem')
export class OverItem extends Component {

    private player_id: number;
    private player_name: string = "";
    private avatar_url: string = "";
    private player_score: number = 0;
    private player_seat_id: number = 0;

    private headBlueBg: Node = null;
    private headRedBg: Node = null;
    private nameLabel: Label = null;
    private scoreLabel: Label = null;
    private headSpr: Sprite = null;
    private head_default: Node = null;
    private mvp: Node = null;
    private btnAttention: Node = null;

    protected onLoad(): void {
        this.headBlueBg = this.node.getChildByName("img_head_blue_bg");
        this.headRedBg = this.node.getChildByName("img_head_red_bg");
        this.nameLabel = this.node.getChildByName("nameLabel").getComponent(Label);
        this.scoreLabel = this.node.getChildByName("scoreLabel").getComponent(Label);
        this.headSpr = this.node.getChildByName("mask").children[0].getComponent(Sprite);
        this.head_default = this.node.getChildByName("img_default_head");
        this.mvp = this.node.getChildByName("img_mvp");
        this.btnAttention = this.node.getChildByName("btnAttention");
    }

    public initItem(id: number, name: string, avatar_url: string, score?: number, seatId?: number) {
        this.player_id = id;
        this.player_name = name;
        this.avatar_url = avatar_url;
        this.player_score = score ? score : 0;
        this.player_seat_id = seatId ? seatId : 0;
    }

    start() {
        this.initContent();
    }

    private initContent() {
        this.mvp.active = false;
        this.btnAttention.active = this.player_id !== NetInfo.uid;
        this.nameLabel.string = this.player_name;
        this.scoreLabel.string = "" + this.player_score;
        if (App.gameData.getModelData() === 1) {
            this.scoreLabel.node.active = true;
        } else {
            this.scoreLabel.node.active = false;
        }
        if (this.player_seat_id <= 2) {
            this.headBlueBg.active = true;
            this.headRedBg.active = false;
        } else {
            this.headBlueBg.active = false;
            this.headRedBg.active = true;
        }
        this.head_default.active = false;
        this.headSpr.node.parent.active = true;
        ResLoadHelper.loadAssetAsync(this.avatar_url, SpriteFrame, (frame: SpriteFrame) => {
            if (frame) {
                this.headSpr.spriteFrame = frame;
            } else {
                this.head_default.active = true;
                this.headSpr.node.parent.active = false;
            }
        }, true)
    }

    update(deltaTime: number) {

    }
}


